Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

184
Views
"can't access variable before initialization" if I break the code into separate function

I've found several articles, both here on SO and elsewhere that mention this error message, but none that I can apply to my specific situation. Can anyone help me understand what's going on?

If I write my code like so:

selDocument.group_selected = function ($, anchor) {
    let tmpitems = $(".chkSelectedField:checked");
    if (tmpitems.length > 1) {
        let items = [];
        $(tmpitems).each(function (a, b) {
            items.push($(b).val());
        });
        if (typeof anchor != 'undefined') {
            items = items.filter(el => el != anchor);
        } else {
            anchor = items.shift();
        }
        console.log("57199516 items, anchor", items, anchor);

it works fine, but if I take those blocks and break them into their own function:

selDocument.group_selected = function ($, anchor) {
        let tmpitems = $(".chkSelectedField:checked");
        if (tmpitems.length > 1) {
            let retval = selDocument.create_items_array($, tmpitems, anchor);
            let items = retval.items;
            anchor = retval.anchor;
            console.log("57199516 items, anchor", items, anchor);
...
selDocument.create_items_array = function ($, tmpitems, anchor) {
    let items = [];
    $(tmpitems).each(function (a, b) {
        items.push($(b).val());
    });
    if (typeof anchor != 'undefined') {
        items = items.filter(el => el != anchor);
    } else {
        anchor = items.shift();
    }
    return JSON.parse({ 'items': items, 'anchor': anchor });
};

I get "Cannot access 'anchor' before initialization."

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!